import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '物料名称',		name: 'material', 		type:'select', loading:true },
			{ label: '检测组',		name: 'pproject', 		type:'select', loading:true, idStr:'uuid' },
			{ label: '领取人',		name: 'receive_user'},
			{ label: '出库时间',		names: ['start_date','end_date'],	type:'date-range' },
		],
		// 提交表单列表
		formData : [
			{ label: '申请单',		name: 'apply_form_id', 		half:true, 	required:true, 	type:'select', idStr:'uuid', onChange:({value})=>{
				const { formData  } = this.state
				const out_code = $fn.getRow(formData,'out_code')
				out_code.disabled = !Boolean(value)
				this.setState({ formData })
			} },
			{ label: '物料名称',		name: 'mate_id', 			half:true, 	required:true, 	type:'select' },
			{ label: '出库数量',		name: 'out_qty', 			half:true , required:true,	type:'number' },
			{ label: '领取人',		name: 'receive_user', 		half:true, 	required:true },
			{ label: '检测组',		name: 'pgroup_id', 			half:true, 	required:true, 	type:'select', idStr:'uuid' },
			{ label: '审核人',		name: 'reviewer', 			half:true, 	required:true },
			{ label: '出库码',		name: 'out_code', 			half:true, 	required:true, disabled:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = async () => {
		const { forms,formData } = this.state
		$cache.then( async f => {
			// 物料名称
			const _d = await f.getCache({ api:'material' } )
			$fn.setRow(forms, 'material', _d )
			$fn.setRow(formData, 'mate_id', _d )
			//检测组
			const _k = await f.getCache({ api:'ke_shi' } )
			$fn.setRow(forms, 'pproject',  _k)
			$fn.setRow(formData, 'pgroup_id', _k )
			//
			this.setState({ forms })
		})
	}
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, [ 'apply_form_id' ], ()=>{
			$cache.then( async f => {
				// 申请单
				$fn.setRow(formData, 'apply_form_id', await f.getCache({ api:'mt-apply-form/select' } ) )
				
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'material-out/index')
	
	// 表单
	cols = [
		{ title: '申请单编号', 	dataIndex: 'apply_number', 		width:150 },
		{ title: '出库单编号', 	dataIndex: 'out_form_number', 	width:200 },
		{ title: '物料名称', 	dataIndex: 'mate_name', 		width:200 },
		{ title: '物料编号', 	dataIndex: 'mate_number', 		width:150 },
		{ title: '出库量', 		dataIndex: 'out_qty', 			width:150 },
		{ title: '出库时间', 	dataIndex: 'out_time', 			...$fn.table.time },
		{ title: '出库码', 		dataIndex: 'out_code', 			width:150 },
		{ title: '审核人', 		dataIndex: 'reviewer', 			...$fn.table.name },
		{ title: '检测组', 		dataIndex: 'project_name', 		width:150 },
		{ title: '领取人', 		dataIndex: 'receive_user', 		...$fn.table.name },
	]
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this, { click: this.getModalSelect }),
			Controls.pushTo.call(this,'查看申请' , `controlla-domande` ),
		]
	}
	
	render(){
		return (
			<>
				<Page title='物料出库记录列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { type: -1, isInit:window.isInit }) }
				</Page>
				{/* 添加 */}
				{ AddEditModal.call(this,{ labelWidth:'s6', width:800, api: 'material-out/add'}) }
			</>
		)
	}
}

export default Index